# -*- coding: utf-8 -*-

from optparse import OptionParser
from panjuewenshu.items import Wenshu
import json
import logging

logger = logging.getLogger(__name__)

class Loader(object):

    def __init__(self, filename):
        self.filename = filename

    def __iter_items_from_file(self):
        with open(self.filename) as f:
            for line in f.readlines():
                try:
                    line = line.strip('\n')
                    if line.endswith('"'):
                        line = line[:-1].replace('\\"', '"')
                    yield json.loads(line)
                except Exception, e:
                    print '-'*30
                    logger.info('failed to parse line : %s, error : %s' % (line, e))
                    continue

    def process(self):
        for items in self.__iter_items_from_file():
            for item in items:
                if 'Count' in item:
                    continue

                wenshu = Wenshu(
                    title = item.get(u'案件名称'),
                    anhao = item.get(u'案号'),
                    docid = item.get(u'文书ID'),
                    date = item.get(u'裁判日期'),
                )
                wenshu.save()


def main(filename):
    loader = Loader(filename)
    loader.process()

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    parser = OptionParser()
    parser.add_option('-f', '--filename', dest='filename',
                      help='source file name')
    (options, args) = parser.parse_args()
    main(options.filename)
